package com.doeis.manage.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.doeis.common.Constant;
import com.doeis.core.SuperAction;
import com.doeis.core.bean.Msg;
import com.doeis.manage.bean.Page;
import com.doeis.manage.bean.User;
import com.doeis.manage.service.PageService;
import com.doeis.tools.DateHelper;
import com.doeis.tools.StringHelper;

/**
 * 
 * @author wxy
 * @date 2016-1-6
 */
@WebServlet(urlPatterns = "/page/*")
public class PageAction extends SuperAction {

	private static final long serialVersionUID = 1L;

	private PageService pageService = new PageService();

	@Override
	protected void doRequest(String methodName, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if ("index".equals(methodName)) {
			toIndex(request, response);
		} else if ("edit".equals(methodName)) {
			if (checkSession(request, response)) {
				edit(methodName, request, response);
			} else {
				toPage("index", "index", request, response);
			}
		} else if ("save".equals(methodName)) {
			if (checkSession(request, response)) {
				save(methodName, request, response);
			} else {
				toPage("index", "index", request, response);
			}
		} else if ("delete".equals(methodName)) {
			if (checkSession(request, response)) {
				delete(methodName, request, response);
			} else {
				toPage("index", "index", request, response);
			}
		} else {
			toPage(methodName, "item", request, response);
		}
	}


	private void delete(String methodName, HttpServletRequest request, HttpServletResponse response) {
		try {
			Msg msg = new Msg(false, "删除失败");
			String page_id = request.getParameter("page_id");
			
			if(StringHelper.isNotNull(page_id)){
				Page page = pageService.findById(Integer.parseInt(page_id));
				Integer user_id = getUserid(request);
				if (page!=null&&user_id!=null&&user_id.equals(page.getAuthor_uid())) {
					pageService.delete(page.getId());
					msg.setStatus(true);
					msg.setMsg("删除成功");
				}else{
					msg.setMsg("页面不存在");
				}
			}else{
				msg.setMsg("页面不存在");
			}
			//toPage("index", "item", request, response);
			sendJsonObject(msg, response);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	private void save(String methodName, HttpServletRequest request, HttpServletResponse response) {
		try {
			String page_id = request.getParameter("page_id");
			String cat_id = request.getParameter("cat_id");
			String sort_num = request.getParameter("sort_num");
			String page_content = request.getParameter("page_content");
			String page_title = request.getParameter("page_title");
			String item_id = request.getParameter("item_id");
			User user = (User)request.getSession().getAttribute(Constant.SYSTEM_CURRENT_USER);
			Msg msg = new Msg(false, "保存失败");
			if(user==null){
				msg.setMsg("用户没有登录");
				sendJsonObject(msg, response);
				return;
			}
			Page page = new Page();
			if (StringHelper.isNotNull(page_id)) {
				page = pageService.findById(Integer.parseInt(page_id));
				if(page!=null){
					page.setCat_id(Integer.parseInt(cat_id));
					page.setPage_content(page_content);
					page.setPage_title(page_title);
					page.setSort_num(sort_num==null?0:Integer.parseInt(sort_num));
					pageService.update(page);
					msg.setStatus(true);
					msg.setMsg("更新成功");
				}else{
					msg.setMsg("page不存在");
				}
			}else{
				page = new Page();
				page.setAddTime(DateHelper.currentDateTime());
				page.setAuthor_uid(user.getId());
				page.setAuthor_username(user.getUsername());
				page.setCat_id(Integer.parseInt(cat_id));
				page.setItem_id(Integer.parseInt(item_id));
				page.setPage_content(page_content);
				page.setPage_title(page_title);
				page.setSort_num(sort_num==null?0:Integer.parseInt(sort_num));
				pageService.add(page);
				msg.setStatus(true);
				msg.setMsg("添加成功");
			}
			msg.setObj(page);
			sendJsonObject(msg, response);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void edit(String methodName, HttpServletRequest request, HttpServletResponse response) {
		try {
			String page_id = request.getParameter("page_id");
			String copy_page_id = request.getParameter("copy_page_id");
			if(StringHelper.isNull(page_id)){//新建
				
			}else{//修改
				Page page = pageService.findById(Integer.parseInt(page_id));
				request.setAttribute("page1", page);
			}
			if(StringHelper.isNotNull(copy_page_id)){//新建
				Page page = pageService.findById(Integer.parseInt(copy_page_id));
				request.setAttribute("page1", page);
			}
			
			
			toPage(methodName, "page", request, response);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 跳转到管理页面
	 * 
	 * @Title: toIndex
	 * @data:2014-1-6上午09:40:37
	 * @author:xieyan
	 *
	 * @param request
	 * @param response
	 */
	private void toIndex(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String page_id = request.getParameter("page_id");
			Page page = pageService.findById(Integer.parseInt(page_id));
			request.setAttribute("page", page);
			request.getRequestDispatcher(PAGE_PATH + "/page/index.jsp").forward(request, response);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
